home *** CD-ROM | disk | FTP | other *** search
- package
- {
- import com.plus9.mockups.model.ICookieManager;
- import com.plus9.mockups.model.IStorageManager;
- import flash.data.SQLConnection;
- import flash.data.SQLMode;
- import flash.data.SQLStatement;
- import flash.filesystem.File;
-
- public class LocalDBManager implements ICookieManager
- {
- protected var _dbFile:File;
-
- protected var _sqlConnection:SQLConnection;
-
- protected var _useIt:Boolean = true;
-
- protected var _sm:IStorageManager;
-
- public function LocalDBManager()
- {
- var _loc1_:SQLStatement = null;
- super();
- _dbFile = File.applicationStorageDirectory.resolvePath("MockupsCookies.db");
- _sqlConnection = new SQLConnection();
- if(!_dbFile.exists)
- {
- _sqlConnection.open(_dbFile,SQLMode.CREATE);
- _loc1_ = new SQLStatement();
- _loc1_.sqlConnection = _sqlConnection;
- _loc1_.text = "CREATE TABLE cookies (propName TEXT PRIMARY KEY, propValue OBJECT);";
- _loc1_.execute();
- _sqlConnection.close();
- }
- }
-
- public function getProperty(param1:String, param2:Object) : Object
- {
- var res:Object = null;
- var sqlStatement:SQLStatement = null;
- var results:Array = null;
- var temp:Object = null;
- var p_propName:String = param1;
- var p_default:Object = param2;
- if(_useIt == false)
- {
- return p_default;
- }
- _sqlConnection = new SQLConnection();
- try
- {
- _sqlConnection.open(_dbFile,SQLMode.READ);
- sqlStatement = new SQLStatement();
- sqlStatement.sqlConnection = _sqlConnection;
- sqlStatement.text = "SELECT propValue FROM cookies WHERE propName=\'" + p_propName + "\';";
- sqlStatement.execute();
- results = sqlStatement.getResult().data;
- if(results == null)
- {
- res = p_default;
- }
- else
- {
- temp = results[0];
- res = temp.propValue;
- }
- }
- catch(e:Error)
- {
- log("SELECT failed!:" + e.toString());
- res = p_default;
- }
- _sqlConnection.close();
- if(res == "true")
- {
- res = true;
- }
- else if(res == "false")
- {
- res = false;
- }
- log("#LDBMGR# getProperty " + p_propName + ", " + p_default + ", returning " + res);
- return res;
- }
-
- public function set storageManager(param1:IStorageManager) : void
- {
- _sm = param1;
- }
-
- public function get useCookies() : Boolean
- {
- return _useIt;
- }
-
- protected function dumpDB() : void
- {
- }
-
- public function set useCookies(param1:Boolean) : void
- {
- _useIt = param1;
- }
-
- protected function log(param1:String) : void
- {
- if(_sm)
- {
- _sm.log(param1);
- }
- }
-
- public function setProperty(param1:String, param2:Object) : void
- {
- var valueToAdd:Object;
- var sqlStatement:SQLStatement = null;
- var p_propName:String = param1;
- var p_value:Object = param2;
- if(_useIt == false)
- {
- return;
- }
- log("#LDBMGR# setProperty: " + p_propName + ", " + p_value);
- valueToAdd = p_value;
- if(p_value is Boolean)
- {
- valueToAdd = p_value + "";
- }
- else
- {
- valueToAdd = p_value;
- }
- _sqlConnection = new SQLConnection();
- try
- {
- _sqlConnection.open(_dbFile,SQLMode.UPDATE);
- sqlStatement = new SQLStatement();
- sqlStatement.sqlConnection = _sqlConnection;
- sqlStatement.text = "UPDATE cookies SET propValue=? WHERE propName=\'" + p_propName + "\';";
- sqlStatement.parameters[0] = valueToAdd;
- sqlStatement.execute();
- if(sqlStatement.getResult().rowsAffected == 0)
- {
- log("#LDBMGR# inserting instead!");
- sqlStatement.text = "INSERT INTO cookies (propName, propValue) VALUES (\'" + p_propName + "\',?);";
- sqlStatement.parameters[0] = valueToAdd;
- sqlStatement.execute();
- }
- }
- catch(e:Error)
- {
- log("UPDATE failed!:" + e.toString());
- }
- _sqlConnection.close();
- }
- }
- }
-
-